/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/6/15 17:37
  *
  * @file       hd_motor.h
  * @brief      Header file of Motor hardware driver.
  *
  * @note       This file contains the Xxx object declaration.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date          Author       Notes
  *   2024/6/15     Chris        the first version
  *
  ******************************************************************************
  */


#ifndef JLC_SKY_HD_MOTOR_H
#define JLC_SKY_HD_MOTOR_H

#include "stm32f407xx.h"
#include "pd_timer_pwm.h"

struct MotorVtbl;

/** @defgroup Object properties
  * @{
  */
typedef struct {
    TimerPWM *timerPwm; /* PWM signal input port */
    GPIO *in1GPIO;      /* AIN1 input port       */
    GPIO *in2GPIO;      /* AIN2 input port       */
    int8_t speed;       /* motor speed           */

    // Motor virtual table
    struct MotorVtbl *vptr;
} Motor;
/**
 * @}
 */

/** @defgroup Object virtual table definition
  * @{
  */
struct MotorVtbl {
    void (*init)(const Motor *this);

    void (*start)(const Motor *this);

    void (*stop)(const Motor *this);

    void (*set_speed)(const Motor *this, int8_t speed);
};
/**
 * @}
 */

/** @defgroup Object constructor and member function declaration
  * @{
  */
void Motor_constructor(Motor *this, TimerPWM *timerPwm, GPIO *in1GPIO, GPIO *in2GPIO);
/**
 * @}
 */

/** @defgroup Object virtual table function implementation
  * @{
  */
static inline void Motor_init(const Motor *this) {
    this->vptr->init(this);
}

static inline void Motor_start(const Motor *this) {
    this->vptr->start(this);
}

static inline void Motor_stop(const Motor *this) {
    this->vptr->stop(this);
}

static inline void Motor_setSpeed(const Motor *this, int8_t speed) {
    this->vptr->set_speed(this, speed);
}
/**
 * @}
 */


#endif // JLC_SKY_HD_MOTOR_H
